# Who Cares if You Vote? Partisan agreement and injunctive norms of voting
# Edward Fieldhouse (1), David Cutts (2), Jack Bailey (1)
# (1) The University of Manchester, (2) The University of Birmingham


# 1. Housekeeping ---------------------------------------------------------

# The following code requires that you have first opened the associated
# project file. If not, click the button on the top right and open it
# ("Open Project...").

# Note also that the Bayesian methods we use below rely on having installed
# the probabilistic programming language Stan. If you have yet to install
# Stan, please see https://mc-stan.org/users/interfaces/.

# Load packages

library(tidyverse)
library(magrittr)
library(jbmisc)   # devtools::install_github("jackobailey/jbmisc)
library(haven)
library(here)


# Load data from the British Election Study Internet Panel. Note: If you
# plan to use this data, please make sure that you download the most recent
# version available at https://www.britishelectionstudy.com.

dta <- read_sav(here("_data", "BES2017_W14_Panel_v0.4.sav.zip"))


# Merge in wave 12 randomisation variable.

dta <- 
  left_join(
    dta,
    read_sav(here("_data", "randvar_w12.sav.zip")),
    by = "id"
  )



# 2. Transform data -------------------------------------------------------

# Select variables

dta <- 
  dta %>% 
  select(
    # Respondent-level covariates
    id,
    wave2,
    wave12,
    pid = partyIdW2,
    rnd = getsBrandenburg,
    
    # Discussant-level covariates
    vote = discussantVoteName1W2,
    vote_1W12 = discussantVoteName1W12,
    vote_2W12 = discussantVoteName2W12,
    vote_3W12 = discussantVoteName3W12
  )


# Party ID

dta$pid <-
  dta$pid %>%
  as_factor() %>%
  factor(
    labels = c("Conservative",
               "Labour",
               "Liberal Democrat",
               "SNP",
               "Plaid Cymru",
               "UKIP",
               "Green Party",
               "BNP",
               "Other",
               "None",
               "Don't know")
  )


# Discussant vote intention

dta$vote <-
  dta$vote %>%
  as_factor() %>%
  factor(
    labels = 
      c("Labour",
        "Conservative",
        "Liberal Democrat",
        "SNP",
        "Plaid Cymru",
        "Green Party",
        "UKIP",
        "BNP",
        "Other",
        "None",
        "Don't know")
  )


# Create dyadic partisanship variable

opt <- c("None", "Don't know")

dta <- 
  dta %>% 
  mutate(
    dyad =
      case_when(
        pid == vote & !(pid %in% opt) & !(vote %in% opt) & pid != "Other" & vote != "Other" ~ "Shared Partisanship",
        pid != vote & !(pid %in% opt) & !(vote %in% opt) ~ "Opposing Partisanship",
        pid == "Other" & vote == "Other" ~ "Opposing Partisanship",
        pid %in% opt & !(vote %in% opt) ~ "Respondent Non-partisan",
        vote %in% opt & !(pid %in% opt) ~ "Discussant Non-partisan",
        pid %in% opt & vote %in% opt ~ "Shared Non-partisan"
      ) %>% 
      factor(
        levels = 
          c(
            "Shared Non-partisan",
            "Shared Partisanship",
            "Opposing Partisanship",
            "Respondent Non-partisan",
            "Discussant Non-partisan"
          )
      )
  )


# Count wave 12 discussants and drop vars

dta <- 
  dta %>% 
  mutate(
    discW12 = 
      3 %>% 
      subtract(
        dta[, paste0("vote_", 1:3, "W12")] %>% 
          is.na() %>% 
          rowSums()
      ) %>% 
      ifelse(rnd == 1, ., NA)
  ) %>% 
  mutate(discW12 = ifelse(discW12 > 0, 1, 0)) %>% 
  select(
    -vote_1W12,
    -vote_2W12,
    -vote_3W12
  )



# 3. Create tables --------------------------------------------------------

# Create wave 12 table

w12_table <- 
  dta %>% 
  group_by(dyad) %>% 
  summarise(
    `W12 Survey Non-Response` = scales::percent(1 - round(mean(wave12), 3), accuracy = 0.1),
    `W12 Item Non-Response` = scales::percent(1 - round(mean(discW12, na.rm = T), 2), accuracy = 0.1),
    .groups = "drop"
  ) %>% 
  filter(is.na(dyad) == F) %>% 
  rename(`Dyadic Partisanship` = dyad)


# Save to disk

write_csv(w12_table, here("_output", "attritionW12.csv"))

